package qcmPlus.tests.java;

import java.util.ArrayList;

import org.dbunit.Assertion;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.ITable;
import org.hibernate.Session;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import qcmPlus.beans.Questionnaire;
import qcmPlus.dao.QuestionnaireDao;
import qcmPlus.tools.HibernateUtil;

public class QuestionnaireDaoTest extends AbstractTest {

	private QuestionnaireDao _questionnaireDao = new QuestionnaireDao();
	private Questionnaire _questionnaire;
	private String[] ignore = { "ID_QUESTIONNAIRE" };
	private String nom = "test";
	private String description = "junit";
	private String urlActual;
	private String urlExpected;
	private ITable actualTable;
	private ITable expectedTable;
	private IDataSet expectedDataSet;
	String nomTable = "Questionnaire";

	public QuestionnaireDaoTest() throws Exception {
		super();
	}

	@Before
	public void setUp() throws Exception {
		new QuestionnaireDaoTest();
	}

	@Test
	public void testListerQuestionnaires() throws Exception {

		urlActual = "./src/qcmPlus/tests/datas/questionnaire/lister-dataset.xml";
		urlExpected = "./src/qcmPlus/tests/datas/questionnaire/lister-expected.xml";

		initial(urlActual);

		actualTable = getActualTable("Questionnaire");
		expectedDataSet = getExpectedDataset(urlExpected);
		expectedTable = getExpectedTable(expectedDataSet, nomTable);

		ArrayList<Questionnaire> liste = _questionnaireDao.list();

		Assert.assertTrue(liste.size() == 5);
		Assertion.assertEqualsIgnoreCols(expectedTable, actualTable, ignore);

	}

	@Test
	public void testInsertionQuestionnaire() throws Exception {
		clean("Questionnaire");

		urlActual = "./src/qcmPlus/tests/datas/questionnaire/insertion-dataset.xml";
		urlExpected = "./src/qcmPlus/tests/datas/questionnaire/insertion-expected.xml";

		initial(urlActual);

		_questionnaire = new Questionnaire(nom, description);
		_questionnaireDao.insertion(_questionnaire.getNom(),
				_questionnaire.getDescription());

		actualTable = getActualTable("questionnaire");
		expectedDataSet = getExpectedDataset(urlExpected);
		expectedTable = getExpectedTable(expectedDataSet, nomTable);

		Assertion.assertEqualsIgnoreCols(expectedTable, actualTable, ignore);
	}

	@Test
	public void testgetQuestionnaire() throws Exception {
		urlActual = "./src/qcmPlus/tests/datas/questionnaire/getQuestionnaire-dataset.xml";

		initial(urlActual);

		Questionnaire q = _questionnaireDao.getQuestionnaire(57);

		Assert.assertNotNull(q);
		Assert.assertEquals(q.getNom(), "get");
		Assert.assertEquals(q.getDescription(), "a avoir");
	}

	@Test
	public void testModificationQuestionnaire() throws Exception {
		urlActual = "./src/qcmPlus/tests/datas/questionnaire/modification-dataset.xml";
		urlExpected = "./src/qcmPlus/tests/datas/questionnaire/modification-expected.xml";

		initial(urlActual);

		Session s = HibernateUtil.currentSession();
		String query = "from Questionnaire where NOM= 'modif'";

		Questionnaire q = (Questionnaire) s.createQuery(query).list().get(0);

		q.setDescription("ok fait");
		_questionnaireDao.modification(q);

		actualTable = getActualTable("questionnaire");
		expectedDataSet = getExpectedDataset(urlExpected);
		expectedTable = getExpectedTable(expectedDataSet, nomTable);

		Assertion.assertEqualsIgnoreCols(expectedTable, actualTable, ignore);
	}

	@SuppressWarnings("unchecked")
	@Test
	public void testSuppressionQuestionnaire() throws Exception {
		urlActual = "./src/qcmPlus/tests/datas/questionnaire/suppression-dataset.xml";

		initial(urlActual);

		Session s = HibernateUtil.currentSession();
		String query = "from Questionnaire where NOM= 'sup'";
		Questionnaire q2 = (Questionnaire) s.createQuery(query).list().get(0);
		Assert.assertNotNull(q2);
		Assert.assertEquals(q2.getNom(), "sup");
		Assert.assertEquals(q2.getDescription(), "supprime");
		_questionnaireDao.suppression(q2);
		s = HibernateUtil.currentSession();
		ArrayList<Questionnaire> list = (ArrayList<Questionnaire>) s
				.createQuery(query).list();
		Assert.assertTrue(list.size() == 0);
		s.close();
	}

}
